package com.webinsky.article.persist.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class ArticleRelationshipMapperProvider {
	
	private static final String SELECT_TEMPLATE = "select id, site_id, type, cn_id, jp_id, en_id, kr_id, th_id, tw_id, create_at from `relationship`";
	
	@SuppressWarnings("unchecked")
	public String selectBySiteIdAndType(Map<String, Object> params){
		Integer siteId = (Integer)params.get("siteId");
		List<String> types = (List<String>)params.get("types");
		List<String> conditions = new ArrayList<>();
		if ( siteId > 0 ){
			conditions.add("site_id = " + siteId);
		}
		if ( types != null && !types.isEmpty() ){
			conditions.add("type in ('" + String.join(",'", types) + "')");
		}
		if ( conditions.isEmpty() ) {
			return SELECT_TEMPLATE;
		}
		return SELECT_TEMPLATE + " where " + String.join(" and ", conditions);
	}
	
	@SuppressWarnings("unchecked")
	public String selectByCnIds(Map<String, Object> params){
		List<Integer> cnIds = (List<Integer>)params.get("cnIds");
		String condition = "";
		if ( cnIds != null && !cnIds.isEmpty() ){
			List<String> strCnIds = cnIds.stream().map(e -> String.valueOf(e)).collect(Collectors.toList());
			condition = "where cn_id in (" + String.join(",", strCnIds) + ")";
		}
		return SELECT_TEMPLATE + condition;
	}
	
}
